package com.business.action.oa;

import com.business.bean.oa.LeaveApplyBean;
import com.business.bean.oa.WorkApplyTime;
import com.business.bean.system.UserBean;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 离职接口
 * Created by jiangwenfeng on 2019/10/10.
 */
@Controller
@RequestMapping("/oa/quit")
public class QuitAction {

     /**
       * @description 模糊查询用户
       * @author  jiangwenfeng
       * @date 创建时间：2019/10/10 0:41
      */
     @ResponseBody
     @RequestMapping("/getUserList.do")
     public PageBean getUserList(HttpServletRequest request, Service service) {
         PageBean page = WebUtils.getPageBean(request);
         Map<String, Object> map = WebUtils.getRequestData(request);
         BeanDAO dao = new BeanDAO(service);
         BeanSQL query = dao.getQuerySQL();
         query.setPage(page);
         query.setEntityClass(UserBean.class);
         Object key = map.get("key");
         String sql = "select * from SYS_USER_VIEW where userName LIKE ? or workNo LIKE ?";
         if (map.get("key")!=null){
             query.setSQL(sql);
             key = "%"+key+'%';
             query.addParameter(key);
             query.addParameter(key);
         }else {
             query.createSql();
         }
         dao.list();
         return page;
     }

    @ResponseBody
    @RequestMapping("/getUserList1.do")
    public PageBean getUserList1(HttpServletRequest request, Service service) {
        PageBean page = WebUtils.getPageBean(request);
        Map<String, Object> map = WebUtils.getRequestData(request);
        Object userName = map.get("key");
        if (userName != null) {
            map.put("userName_link", "like");
            map.put("userName",userName);
        }
        BeanDAO dao = new BeanDAO(service);
        BeanSQL query = dao.getQuerySQL();
        query.setPage(page);
        query.setEntityClass(UserBean.class);
        query.setTableName("SYS_USER_VIEW");
        query.createSql(map);
        dao.list();
        return page;
    }

     /**
       * @description 根据部门查询用户
       * @author  jiangwenfeng
       * @date 创建时间：2019/10/12 21:41
      */
     @ResponseBody
     @RequestMapping("/getUserListByDepart.do")
     public List<UserBean> getUserListByDepart(HttpServletRequest request, Service service) {
         Map<String, Object> map = WebUtils.getRequestData(request);
         BeanDAO dao = new BeanDAO(service);
         BeanSQL query = dao.getQuerySQL();
         query.setEntityClass(UserBean.class);
         query.createSql(map);
         return dao.list();
     }

     /**
       * @description 查询自己及自己的下属
       * @author  jiangwenfeng
       * @date 创建时间：2019/10/12 16:29
      */
    @ResponseBody
    @RequestMapping("/getUserListByLeader.do")
    public List<UserBean> getUserListByLeader(HttpServletRequest request, Service service) {
        Map<String, Object> map = WebUtils.getRequestData(request);
        BeanDAO dao = new BeanDAO(service);
        BeanSQL query = dao.getQuerySQL();
        query.setEntityClass(UserBean.class);
        StringBuilder sql = new StringBuilder();
        sql.append("select t.* from ");
        sql.append("(select t1.* from SYS_USER t1 LEFT JOIN sys_user t2 on t1.leaderID=t2.userID where 1=1");
        if(map.get("userID")!=null&&!"".equals(map.get("userID").toString())){
            sql.append(" and t2.userID=? ");
        }
        sql.append(" UNION ALL ");
        sql.append(" select t3.* from SYS_USER t3 where 1=1 ");
        if(map.get("userID")!=null&&!"".equals(map.get("userID").toString())){
            sql.append(" and t3.userID=? ");
        }
        sql.append(") t");

        query.setSQL(sql.toString());
        if(map.get("userID")!=null&&!"".equals(map.get("userID").toString())){
            query.addParameter(map.get("userID").toString());
            query.addParameter(map.get("userID").toString());
        }
        return dao.list();
    }

     /**
       * @description 列表
       * @author  jiangwenfeng
       * @date 创建时间：2019/10/11 0:46
      */
     @ResponseBody
     @RequestMapping("/page.do")
     public PageBean page(HttpServletRequest request, Service service) {
         PageBean page = WebUtils.getPageBean(request);
         Map<String, Object> map = WebUtils.getRequestData(request);
         BeanDAO dao = new BeanDAO(service);
         BeanSQL query = dao.getQuerySQL();
         query.setPage(page);
         query.setEntityClass(LeaveApplyBean.class);
         query.setTableName("OA_LEAVE_APPLY_VIEW");
         query.createSql(map);
         dao.list();
         return page;
     }

     /**
       * @description 保存
       * @author  jiangwenfeng
       * @date 创建时间：2019/10/11 0:39
      */
     @ResponseBody
     @RequestMapping("/save.do")
     public PageBean save(Service service, LeaveApplyBean bean) {
         PageBean res = new PageBean();
         BeanDAO dao = new BeanDAO(service);
         BeanSQL query = dao.getQuerySQL();
         query.setEntityClass(UserBean.class);
         query.createSql(bean.getUserID());
         UserBean user = dao.get();
         bean.setWorkNo(user.getWorkNo());
         bean.setUserName(user.getUserName());
         bean.setDepartID(user.getDepartID());

         if (DBUtils.update(service, bean) > 0) {
             res.setMessage("保存信息成功！");
         } else {
             res.setSuccess(false);
             res.setMessage("保存信息失败！");
         }
         return res;
     }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete.do")
    public PageBean delete(Service service, String applyID) {
        return DBUtils.delete(service, LeaveApplyBean.class, applyID);
    }

    /**
     * 审批
     */
    @ResponseBody
    @RequestMapping("/check.do")
    public PageBean check(Service service, int status, String userID, String ids) {
        PageBean res = new PageBean();
        String[] arr = StringUtils.split(ids, '.');
        Date d = new Date();
        java.sql.Timestamp today = new java.sql.Timestamp(d.getTime());
        BeanDAO dao = new BeanDAO(service);
        BeanSQL query = dao.getQuerySQL();
        query.setEntityClass(LeaveApplyBean.class);
        query.setSQL("select * from OA_LEAVE_APPLY where applyID=?");
        List<LeaveApplyBean> list = new ArrayList<>();
        for (String id : arr) {
            query.addParameter(id);
            LeaveApplyBean w = dao.get();
            w.setCheckStatus(status);
            w.setCheckTime(today);
            list.add(w);
            query.clearParameterList();
        }
        query.createBatchUpdateSql("checkStatus,checkTime", list);
        int[] is = dao.batchUpdate();
        if (dao.getBatchUpdateRecordCount() > 0) {
            // 如果是审批通过
            if (status == 1) {
				/* WorkDayReportService wrs = new WorkDayReportService(service);
				for (int i = 0; i < list.size(); i++) {
					if (is[i] > 0) {
						WorkApplyTime bean = list.get(i);
						wrs.userApplyAction(bean);
					}
				} */
            }
            // 如果是审批不通过
            else if (status == 2) {

            }
            res.setMessage("保存信息成功！");
        } else {
            res.setSuccess(false);
            res.setMessage("保存信息失败！");
        }
        return res;
    }
}
